package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.File;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.FileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysInstructionMapper;
import com.ruoyi.system.domain.SysInstruction;
import com.ruoyi.system.service.ISysInstructionService;

/**
 * 操作指南Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-19
 */
@Service
public class SysInstructionServiceImpl implements ISysInstructionService
{
    @Autowired
    private SysInstructionMapper sysInstructionMapper;

    @Autowired
    private FileMapper fileMapper;

    /**
     * 查询操作指南
     *
     * @param id 操作指南主键
     * @return 操作指南
     */
    @Override
    public SysInstruction selectSysInstructionById(Long id)
    {
        List<File> fileList = new ArrayList<>();
        SysInstruction sysInstruction = sysInstructionMapper.selectSysInstructionById(id);
        if(sysInstruction != null){
            Integer fileId = sysInstruction.getFileId();
            if(fileId != null){
                //根据fileId 获取文件
                File file = fileMapper.selectFileById(fileId);
                if(file != null){
                    fileList.add(file);
                }
                sysInstruction.setFileList(fileList);
            }
        }
        return sysInstruction;
    }

    /**
     * 查询操作指南列表
     *
     * @param sysInstruction 操作指南
     * @return 操作指南
     */
    @Override
    public List<SysInstruction> selectSysInstructionList(SysInstruction sysInstruction)
    {
        return sysInstructionMapper.selectSysInstructionList(sysInstruction);
    }

    /**
     * 新增操作指南
     *
     * @param sysInstruction 操作指南
     * @return 结果
     */
    @Override
    public int insertSysInstruction(SysInstruction sysInstruction)
    {
        sysInstruction.setCreateTime(DateUtils.getNowDate());
        return sysInstructionMapper.insertSysInstruction(sysInstruction);
    }

    /**
     * 修改操作指南
     *
     * @param sysInstruction 操作指南
     * @return 结果
     */
    @Override
    public int updateSysInstruction(SysInstruction sysInstruction)
    {
        sysInstruction.setUpdateTime(DateUtils.getNowDate());
        return sysInstructionMapper.updateSysInstruction(sysInstruction);
    }

    /**
     * 批量删除操作指南
     *
     * @param ids 需要删除的操作指南主键
     * @return 结果
     */
    @Override
    public int deleteSysInstructionByIds(String[] ids)
    {
        return sysInstructionMapper.deleteSysInstructionByIds(ids);
    }

    /**
     * 删除操作指南信息
     *
     * @param id 操作指南主键
     * @return 结果
     */
    @Override
    public int deleteSysInstructionById(String id)
    {
        return sysInstructionMapper.deleteSysInstructionById(id);
    }

    @Override
    public SysInstruction getInstructionRecently() {
        SysInstruction sysInstruction = sysInstructionMapper.getInstructionRecently();
        if(sysInstruction != null && sysInstruction.getFileId() != null){
            Integer fileId = sysInstruction.getFileId();
            File file = fileMapper.selectFileById(fileId);
            sysInstruction.setFile(file);
        }
        return sysInstruction;
    }

    @Override
    public int updateStatus(SysInstruction sysInstruction) {
        /*
            根据当前状态判断
            1、如果当前状态是已发布，则其他的都是未发布
         */
        String status = sysInstruction.getStatus();
        Long id = sysInstruction.getId();
        if(StringUtils.isNotEmpty(status) && status.equals("1")){ //已发布
            sysInstructionMapper.updateAllStuatus();
            return sysInstructionMapper.updateStatus(id,status);
        }else if(StringUtils.isNotEmpty(status) && status.equals("0")){
            return sysInstructionMapper.updateStatus(id,status);
        }
        return 0;
    }
}
